/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QKeyEvent>
#include <QFocusEvent>

#include "query.h"
#include "combobox.h"

using namespace SQL;

ComboBox::ComboBox(QWidget *parent)
 : QComboBox(parent),m_handleFocus(true)
{
}

bool ComboBox::handleFocus()
{
	return m_handleFocus;
}

void ComboBox::setHandleFocus(bool handleFocus)
{
	m_handleFocus=handleFocus;
}

void ComboBox::keyPressEvent(QKeyEvent * event)
{
	if (event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter)
	{
		setData();
		if (m_handleFocus)
			 focusNextChild();
		emit(returnPressed());
		emit(editingFinished());
	}

	if (event->key()==Qt::Key_Escape)
	{
		if (m_handleFocus)
			 focusPreviousChild();
		emit(escapePressed());
	}
	QComboBox::keyPressEvent(event);
}

void ComboBox::setData()
{
	Query *q = defaultDatabase()->query(m_queryName);
	if (!q || !m_fieldName.length())
		return;
	if (itemData(currentIndex()).isValid())
		q->setValue(m_fieldName,itemData(currentIndex()));
	else
		q->setValue(m_fieldName,currentText());
}

void ComboBox::focusOutEvent(QFocusEvent * event)
{
	if (event->lostFocus())
	{
		emit(editingFinished());
		setData();
	}
	QWidget::focusOutEvent(event);
}


const QString & ComboBox::queryName()
{
	return m_queryName;
}

void ComboBox::setQueryName(const QString & queryName)
{
	m_queryName = queryName;
	Query *q = defaultDatabase()->query(m_queryName);
	if (!q)
		return;

	connect(q, SIGNAL(recordChanged(const QSqlRecord&)), SLOT(recordChanged(const QSqlRecord&)));
	connect(q, SIGNAL(beforeInsert()), SLOT(setReadWrite()));
	connect(q, SIGNAL(beforeUpdate()), SLOT(setReadWrite()));
	connect(q, SIGNAL(afterSelect()), SLOT(setReadOnly()));
	connect(q, SIGNAL(afterClose()), SLOT(setReadOnly()));
	connect(q, SIGNAL(afterPost()), SLOT(setReadOnly()));
	connect(q, SIGNAL(afterCancel()), SLOT(setReadOnly()));
	setDisabled(q->state() == Query::Select);
}

const QString & ComboBox::fieldName()
{
	return m_fieldName;
}

void ComboBox::setFieldName(const QString & fieldName)
{
	m_fieldName = fieldName;
	if (defaultDatabase()->query(m_queryName))
	{
		QSqlRecord record=defaultDatabase()->query(m_queryName)->record();
		int pos=findData(record.value(m_fieldName));
		if (-1==pos)
			pos=findText(record.value(m_fieldName).toString());
		if (-1!=pos)
			setCurrentIndex(pos);
		else
			setEditText(record.value(m_fieldName).toString());
	}
}

void ComboBox::setReadWrite()
{
	setFocusPolicy(Qt::WheelFocus);
	setEnabled(true);
	setData();
}

void ComboBox::setReadOnly()
{
	setFocusPolicy(Qt::NoFocus);
	setDisabled(true);
}

void ComboBox::recordChanged(const QSqlRecord& record)
{
	int pos=findData(record.value(m_fieldName));
	if (-1==pos)
		pos=findText(record.value(m_fieldName).toString());
	if (-1!=pos)
		setCurrentIndex(pos);
	else
		setEditText(record.value(m_fieldName).toString());
}
